/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* removeElements(struct ListNode* head, int val){
    if(!head)
        return NULL;
    struct ListNode* delnode = NULL;
    while(head && (head->val == val)){
        delnode = head;
        head = delnode->next;
        free(delnode);
        delnode = NULL;
    }
    struct ListNode* cur = head;
    while(cur && (cur->next)){
        delnode = cur->next;
        if(delnode->val == val){
           cur->next = delnode->next;
           free(delnode);
           delnode = NULL;
        }
        else
            cur = cur->next;
        
    }
    return head;
}